<template>
    <div class="body">
        <el-dialog
                :title="title"
                :visible.sync="dialogFormVisible"
                width="600px"
                :close-on-click-modal="false"
                @close="close"
        >
            <el-form ref="form" :model="form" label-width="120px">
                <el-form-item label="当前余额" v-if="type=='balance'">
                  <span class="wei subjectcolor2">{{row.balance || 0}}</span> 元
                </el-form-item>
                <el-form-item label="当前积分" v-if="type=='integral'">
                  <span class="wei subjectcolor2">{{row.integral || 0}}</span> 分
                </el-form-item>
                <el-form-item label="变动类型">
                  <el-radio-group v-model="form.type">
                    <el-radio label="1">增加</el-radio>
                    <el-radio label="2">减少</el-radio>
                    <el-radio label="3" v-if="type=='balance'">最终余额</el-radio>
                    <el-radio label="3" v-if="type=='integral'">最终积分</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="变动积分" v-if="type=='integral'">
                    <el-input v-model.trim="form.integral" autocomplete="off" placeholder="请输入积分"></el-input>
                </el-form-item>
                <el-form-item label="变动余额" v-if="type=='balance'">
                    <el-input v-model.trim="form.balance" autocomplete="off" placeholder="请输入余额"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                  <el-input v-model.trim="form.note" type="textarea" autocomplete="off" placeholder="请输入备注"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="medium" @click="close">取 消</el-button>
                <el-button size="medium" type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import {changeInfo} from "@/api/user";
    export default {
        name: "goodlistedit",
        created() {
        },
        data() {
            return {
                form: {
                    balance: "",
                    integral: "",
                    type:'1',
                    note:'',
                },
                type:'',
                row:'',
                // rules: {
                //     username: [
                //         { required: true, trigger: "blur", message: "请输入用户名" },
                //     ],
                //     password: [
                //         { required: true, trigger: "blur", message: "请输入密码" },
                //     ],
                //     email: [{ required: true, trigger: "blur", message: "请输入邮箱" }],
                //     permissions: [
                //         { required: true, trigger: "blur", message: "请选择权限" },
                //     ],
                // },
                title: "",
                dialogFormVisible: false,
            }
        },
        methods: {
            showEdit(row,type) {
                if (!row) {
                    this.title = "添加";
                } else {
                    this.title = "充值";
                    // this.form = Object.assign({}, row);
                    this.form.id = row.id
                    this.type =  type;
                    this.row = row
                }
                this.dialogFormVisible = true;
            },
            close() {
                this.$refs["form"].resetFields();
                this.form = this.$options.data().form;
                this.dialogFormVisible = false;
            },
            save() {
                var params={}
                if(this.type=="integral"){
                    params={
                        id:this.form.id,
                        integral:this.form.integral,
                        type:this.form.type,
                        note:this.form.note,
                    }
                }else if(this.type=="balance"){
                    params={
                        id:this.form.id,
                        balance:this.form.balance,
                        type:this.form.type,
                        note:this.form.note,
                    }
                }
                this.$refs["form"].validate(async (valid) => {
                    if (valid) {
                        const { msg } = await changeInfo(params);
                        this.$baseMessage(msg, "success");
                        this.$emit("fetchData");
                        this.close();
                    } else {
                        return false;
                    }
                });
            },
        }
    };
</script>
